<?php
/*
 * @Author: 哲学家乔布斯 <wuyangii@qq.com>
 * @Date: 2022-10-20 21:41:37
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-11-07 22:37:15
 * @Descripttion: 我的
 */

namespace app\api\controller\user\v1_0;



class User extends Common
{
    protected $noNeedRight = ['index'];
    /**
     * @Author: 哲学家乔布斯 <wuyangii@qq.com>
     * @brief: 
     * @return 
     * @Descripttion: 首页默认接口
     */    
    public function index(){
        $user = (new \app\common\model\User())
                    ->field(['id','nickname','avatar','money','bus_id'])
                    ->where(['id' => $this->UserInfo['id']])
                    ->find();
        $bus = (new \app\common\model\Bus())->where(['user_id' => $this->UserInfo['id']])->find();
        return self::successResult("成功",[
            'user' => $user,
            'is_bus' => !empty($bus) ? 1 : 0,
            'order' => [
                'order_count_0' => (new \app\common\model\UserOrders())->where(['user_id' => $this->UserInfo['id'],'state' => 0])->count(),
                'order_count_1' => (new \app\common\model\UserOrders())->where(['user_id' => $this->UserInfo['id'],'state' => 1])->count(),
                'order_count_2' => (new \app\common\model\UserOrders())->where(['user_id' => $this->UserInfo['id'],'state' => 2])->count(),
                'order_count_3' => (new \app\common\model\UserOrders())->where(['user_id' => $this->UserInfo['id'],'state' => 3])->count(),
                'order_count_4' => (new \app\common\model\UserOrders())->where(['user_id' => $this->UserInfo['id'],'state' => 4])->count(),
                'order_count_5' => (new \app\common\model\UserOrders())->where(['user_id' => $this->UserInfo['id'],'state' => 5])->count(),
            ]
        ]);
    }

    public function edit(){
        $param = request()->param();
        $data=[];
        if(!empty($param['avatar'])){
            $data['avatar'] = $param['avatar'];
        }
        if(!empty($param['nickname'])){
            $data['nickname'] = $param['nickname'];
        }
        (new \app\common\model\User())->where(['id' => $this->UserInfo['id']])->update($data);
        return self::successResult("成功");
    }
 
    public function bus_form_add(){
       
        $param = request()->param();
        $bus = (new \app\common\model\BusForm())->where(['phone' => $param['phone']])->find();
        if(!empty($bus)){
            return self::successResult("该手机号已经提交过了");
        }
        $validate = (new \app\common\validate\BusForm());
        $result = $validate->scene('add')->check($param);
        if ($result === false) {
            self::errorResult($validate->getError());
        }
        $data = (new \app\common\model\BusForm())->insert([
            'account' => $param['phone'],
            'nickname' => $param['nickname'],
            'avatar' => $param['avatar'],
            'agent_id' => $this->agent_id,
            'phone' => $param['phone'],
            'address' => $param['address'],
            'user_id' => $this->UserInfo['id'],
            'latitude' => $param['latitude'],
            'longitude' => $param['longitude'],
            'addtime' => time(),
            'bus_id' => empty($param['bus_id']) ? 0 : $param['bus_id'],
            'state' => 0,
        ]);
   
     
        return self::successResult("成功");
    }
    public function bus_form_index(){
        $param = request()->param();
        $banner_list = (new \app\common\model\Adv())
                ->field(['id','title','image','goods_id'])
                ->append(['image_text'])->hidden(['image'])
                ->where(['agent_id'=>$this->agent_id,'type' => 1])
                ->order('sort asc')
                ->select();
   
        return self::successResult("成功",[
            'ewm' => 'https://xcx.htxxkj.net/ewm.jpg',
            'banners' => $banner_list,
        ]);
    }

}
